using System.Collections.Generic;

namespace ICSharpCode.TextEditor.Document
{
    public class IndentFoldingStrategy : IFoldingStrategy
    {
        private int GetLevel(IDocument document, int offset)
        {
            int i = 0;
            int num = 0;
            checked
            {
                for (int j = offset; j < document.TextLength; num = 0, i++, j++)
                {
                    switch (document.GetCharAt(j))
                    {
                        case ' ':
                            if (++num == 4)
                            {
                                continue;
                            }
                            break;
                        case '\t':
                            continue;
                    }
                    break;
                }
                return i;
            }
        }

        public List<FoldMarker> GenerateFoldMarkers(IDocument document)
        {
            List<FoldMarker> list = new List<FoldMarker>();
            Stack<int> stack = new Stack<int>();
            checked
            {
                for (int i = 0; i < document.TotalNumberOfLines; i++)
                {
                    LineSegment lineSegment = document.GetLineSegment(i);
                    int textLength = document.TextLength;
                    int j;
                    for (j = lineSegment.Offset; j < textLength; j++)
                    {
                        char charAt;
                        if ((charAt = document.GetCharAt(j)) != ' ' && charAt != '\t')
                        {
                            break;
                        }
                    }
                    if (j == textLength)
                    {
                        break;
                    }
                    int num = j - lineSegment.Offset;
                    int num2 = 0;
                    char charAt2 = document.GetCharAt(j);
                    string text = document.GetText(j, lineSegment.Length - num);
                    if (string.IsNullOrEmpty(text))
                    {
                        continue;
                    }
                    switch (charAt2)
                    {
                        case '#':
                            if (text.StartsWith("#region"))
                            {
                                stack.Push(i);
                            }
                            if (text.StartsWith("#endregion") && stack.Count > 0)
                            {
                                num2 = stack.Pop();
                                list.Add(new FoldMarker(document, num2, document.GetLineSegment(num2).Length, i, num + "#endregion".Length));
                            }
                            break;
                        case '/':
                            if (text.StartsWith("/// <summary>"))
                            {
                                stack.Push(i);
                            }
                            if (text.StartsWith("/// </summary>") && stack.Count > 0)
                            {
                                num2 = stack.Pop();
                                list.Add(new FoldMarker(document, num2, document.GetLineSegment(num2).Length, i, num + "/// </summary>".Length));
                            }
                            break;
                        case '{':
                        case '}':
                            text = document.GetText(j, lineSegment.Length - num);
                            if (text.StartsWith("{"))
                            {
                                stack.Push(i);
                            }
                            if (text.StartsWith("}") && stack.Count > 0)
                            {
                                num2 = stack.Pop() - 1;
                                list.Add(new FoldMarker(document, num2, document.GetLineSegment(num2).Length, i, num + "}".Length));
                            }
                            break;
                    }
                }
                return list;
            }
        }

        public List<FoldMarker> GenerateFoldMarkers(IDocument document, string fileName, object parseInformation)
        {
            List<FoldMarker> list = new List<FoldMarker>();
            Stack<int> stack = new Stack<int>();
            checked
            {
                for (int i = 0; i < document.TotalNumberOfLines; i++)
                {
                    LineSegment lineSegment = document.GetLineSegment(i);
                    int textLength = document.TextLength;
                    int j;
                    for (j = lineSegment.Offset; j < textLength; j++)
                    {
                        char charAt;
                        if ((charAt = document.GetCharAt(j)) != ' ' && charAt != '\t')
                        {
                            break;
                        }
                    }
                    if (j == textLength)
                    {
                        break;
                    }
                    int num = j - lineSegment.Offset;
                    int num2 = 0;
                    char charAt2 = document.GetCharAt(j);
                    string text = document.GetText(j, lineSegment.Length - num);
                    if (string.IsNullOrEmpty(text))
                    {
                        continue;
                    }
                    switch (charAt2)
                    {
                        case '#':
                            if (text.StartsWith("#region"))
                            {
                                stack.Push(i);
                            }
                            if (text.StartsWith("#endregion") && stack.Count > 0)
                            {
                                num2 = stack.Pop();
                                list.Add(new FoldMarker(document, num2, document.GetLineSegment(num2).Length, i, num + "#endregion".Length));
                            }
                            break;
                        case '/':
                            if (text.StartsWith("/// <summary>"))
                            {
                                stack.Push(i);
                            }
                            if (text.StartsWith("/// </summary>") && stack.Count > 0)
                            {
                                num2 = stack.Pop();
                                list.Add(new FoldMarker(document, num2, document.GetLineSegment(num2).Length, i, num + "/// </summary>".Length));
                            }
                            break;
                        case '{':
                        case '}':
                            text = document.GetText(j, lineSegment.Length - num);
                            if (text.StartsWith("{"))
                            {
                                stack.Push(i);
                            }
                            if (text.StartsWith("}") && stack.Count > 0)
                            {
                                num2 = stack.Pop() - 1;
                                list.Add(new FoldMarker(document, num2, document.GetLineSegment(num2).Length, i, num + "}".Length));
                            }
                            break;
                    }
                }
                return list;
            }
        }
    }
}
